// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.characters;

import zombie.ai.states.PathFindState;
import zombie.debug.DebugOptions;
import zombie.iso.IsoDirections;
import zombie.iso.Vector2;
import zombie.network.GameClient;
import zombie.vehicles.PathFindBehavior2;

public class MPDebugAI {
    private static final Vector2 tempo = new Vector2();
    private static final Vector2 tempo2 = new Vector2();

    public static IsoPlayer getNearestPlayer(IsoPlayer target) {
        IsoPlayer isoPlayer0 = null;

        for (IsoPlayer isoPlayer1 : GameClient.IDToPlayerMap.values()) {
            if (isoPlayer1 != target && (isoPlayer0 == null || isoPlayer0.getDistanceSq(target) > isoPlayer1.getDistanceSq(target))) {
                isoPlayer0 = isoPlayer1;
            }
        }

        return isoPlayer0;
    }

    public static boolean updateMovementFromInput(IsoPlayer isoPlayer0, IsoPlayer.MoveVars moveVars) {
        if (GameClient.bClient
            && isoPlayer0.isLocalPlayer()
            && (DebugOptions.instance.MultiplayerAttackPlayer.getValue() || DebugOptions.instance.MultiplayerFollowPlayer.getValue())) {
            IsoPlayer isoPlayer1 = getNearestPlayer(isoPlayer0);
            if (isoPlayer1 != null) {
                Vector2 vector20 = new Vector2(isoPlayer1.x - isoPlayer0.x, isoPlayer0.y - isoPlayer1.y);
                vector20.rotate((float) (-Math.PI / 4));
                vector20.normalize();
                moveVars.moveX = vector20.x;
                moveVars.moveY = vector20.y;
                moveVars.NewFacing = IsoDirections.fromAngle(vector20);
                if (isoPlayer1.isTeleporting() || isoPlayer1.getDistanceSq(isoPlayer0) > 10.0F) {
                    isoPlayer0.removeFromSquare();
                    isoPlayer0.setX(isoPlayer1.realx);
                    isoPlayer0.setY(isoPlayer1.realy);
                    isoPlayer0.setZ((float)isoPlayer1.realz);
                    isoPlayer0.setLx(isoPlayer1.realx);
                    isoPlayer0.setLy(isoPlayer1.realy);
                    isoPlayer0.setLz((float)isoPlayer1.realz);
                    isoPlayer0.ensureOnTile();
                } else if (isoPlayer1.getDistanceSq(isoPlayer0) > 5.0F) {
                    isoPlayer0.setRunning(true);
                    isoPlayer0.setSprinting(true);
                } else if (isoPlayer1.getDistanceSq(isoPlayer0) > 2.5F) {
                    isoPlayer0.setRunning(true);
                } else if (isoPlayer1.getDistanceSq(isoPlayer0) < 1.25F) {
                    moveVars.moveX = 0.0F;
                    moveVars.moveY = 0.0F;
                }
            }

            PathFindBehavior2 pathFindBehavior2 = isoPlayer0.getPathFindBehavior2();
            if (moveVars.moveX == 0.0F
                && moveVars.moveY == 0.0F
                && isoPlayer0.getPath2() != null
                && pathFindBehavior2.isStrafing()
                && !pathFindBehavior2.bStopping) {
                Vector2 vector21 = tempo.set(pathFindBehavior2.getTargetX() - isoPlayer0.x, pathFindBehavior2.getTargetY() - isoPlayer0.y);
                Vector2 vector22 = tempo2.set(-1.0F, 0.0F);
                float float0 = 1.0F;
                float float1 = vector21.dot(vector22);
                float float2 = float1 / float0;
                vector22 = tempo2.set(0.0F, -1.0F);
                float1 = vector21.dot(vector22);
                float float3 = float1 / float0;
                tempo.set(float3, float2);
                tempo.normalize();
                tempo.rotate((float) (Math.PI / 4));
                moveVars.moveX = tempo.x;
                moveVars.moveY = tempo.y;
            }

            if (moveVars.moveX != 0.0F || moveVars.moveY != 0.0F) {
                if (isoPlayer0.stateMachine.getCurrent() == PathFindState.instance()) {
                    isoPlayer0.setDefaultState();
                }

                isoPlayer0.setJustMoved(true);
                isoPlayer0.setMoveDelta(1.0F);
                if (isoPlayer0.isStrafing()) {
                    tempo.set(moveVars.moveX, moveVars.moveY);
                    tempo.normalize();
                    float float4 = isoPlayer0.legsSprite.modelSlot.model.AnimPlayer.getRenderedAngle();
                    float4 = (float)((double)float4 + (Math.PI / 4));
                    if ((double)float4 > Math.PI * 2) {
                        float4 = (float)((double)float4 - (Math.PI * 2));
                    }

                    if (float4 < 0.0F) {
                        float4 = (float)((double)float4 + (Math.PI * 2));
                    }

                    tempo.rotate(float4);
                    moveVars.strafeX = tempo.x;
                    moveVars.strafeY = tempo.y;
                } else {
                    tempo.set(moveVars.moveX, -moveVars.moveY);
                    tempo.normalize();
                    tempo.rotate((float) (-Math.PI / 4));
                    isoPlayer0.setForwardDirection(tempo);
                }
            }

            return true;
        } else {
            return false;
        }
    }

    public static boolean updateInputState(IsoPlayer isoPlayer0, IsoPlayer.InputState inputState) {
        if (GameClient.bClient && isoPlayer0.isLocalPlayer() && DebugOptions.instance.MultiplayerAttackPlayer.getValue()) {
            IsoPlayer isoPlayer1 = getNearestPlayer(isoPlayer0);
            inputState.bMelee = false;
            inputState.isAttacking = false;
            inputState.isCharging = false;
            inputState.isAiming = false;
            inputState.bRunning = false;
            inputState.bSprinting = false;
            if (isoPlayer1 != null) {
                inputState.isCharging = true;
                inputState.isAiming = false;
                if (isoPlayer1.getDistanceSq(isoPlayer0) < 0.5F) {
                    inputState.bMelee = true;
                    inputState.isAttacking = true;
                }
            }

            return true;
        } else if (GameClient.bClient && isoPlayer0.isLocalPlayer() && DebugOptions.instance.MultiplayerFollowPlayer.getValue()) {
            IsoPlayer isoPlayer2 = getNearestPlayer(isoPlayer0);
            inputState.bMelee = false;
            inputState.isAttacking = false;
            inputState.isCharging = false;
            inputState.isAiming = false;
            inputState.bRunning = false;
            inputState.bSprinting = false;
            return true;
        } else {
            return false;
        }
    }
}
